package com.detect.core;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.detect.model.FileInfo;
import com.detect.model.HashLevel;

@Service("fileOperator")
public class FileOperatorImpl implements FileOperator {

    @Autowired
    private HashCode hashCode;

    public FileOperatorImpl(HashLevel scanLevel) {
        hashCode = new HashCodeImpl(scanLevel);
    }

    @Override
    public void removeSubStr(String path, String removeStr) {
        int len = removeStr.length();
        File root = new File(path);
        File[] subFiles = root.listFiles();
        for (File temp: subFiles) {
            if (temp.isDirectory())
                continue;
            String fileName = temp.getName();
            if (fileName.length() >= len && fileName.contains(removeStr)) {
                String newName = "";
                if (fileName.startsWith(removeStr))
                    newName = fileName.substring(fileName
                            .lastIndexOf(removeStr) + len);
                else {
                    String before = fileName.substring(0,
                            fileName.indexOf(removeStr));
                    String last = fileName.substring(fileName
                            .lastIndexOf(removeStr) + len);
                    newName = before.concat(last);
                }
                String newPath = temp.getParent() + File.separator + newName;
                temp.renameTo(new File(newPath));
            }
        }
    }

    @Override
    public void replaceSubStr(String path, String replace, String replaced) {
        File root = new File(path);
        File[] subFiles = root.listFiles();
        for (File temp: subFiles) {
            if (temp.isDirectory())
                continue;
            String fileName = temp.getName();
            if (fileName.contains(replace)) {
                String newName = fileName.replace(replace, replaced);
                String newPath = temp.getParent() + File.separator + newName;
                temp.renameTo(new File(newPath));
            }
        }
    }

    @Override
    public void removeArtist(String path) {
        File root = new File(path);
        File[] subFiles = root.listFiles();
        for (File temp: subFiles) {
            if (temp.isDirectory() || !temp.getName().contains("-"))
                continue;
            String fileName = temp.getName();
            String newName = fileName.substring(fileName.indexOf("-") + 1);
            try {
                String newPath = temp.getParent() + File.separator + newName;
                if (newPath.contains("\\\\"))
                    newPath = newPath.replace("\\\\", "\\");
                if (!temp.renameTo(new File(newPath))) {
                    newName = newName + "_repeat";
                    newPath = temp.getParent() + File.separator + newName;
                    temp.renameTo(new File(newPath));
                }
            } catch (Exception e) {

            }
        }
    }

    @Override
    public boolean renameFile(FileInfo fileSrc, String newName) {
        boolean result = true;
        try {
            String newPath = fileSrc.getFilePath().replace(
                    fileSrc.getFileName(), newName);
            if (newPath.contains("\\\\"))
                newPath = newPath.replace("\\\\", "\\");
            File oldFile = new File(fileSrc.getFilePath());
            if (!oldFile.renameTo(new File(newPath))) {
                result = false;
            } else {
                fileSrc.setFileName(newName);
                fileSrc.setFilePath(newPath);
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

}
